<?php
namespace Purse\Controller;
use Common\Controller\CommonController;

/**
 * 钱包模块
 */
class PurseController extends CommonController {
	protected $_purse;
	protected $_purse_turnover;
	protected $_purse_charge;
	protected $_purse_cash;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		
		
		$this->_moduleid = 111;
		$this->_purse = new \Purse\Model\PurseModel();
		$this->_purse_turnover = new \Purse\Model\PurseTurnoverModel();
		$this->_purse_charge = new \Purse\Model\PurseChargeModel();
		$this->_purse_cash = new \Purse\Model\PurseCashModel();
		$this->_cust_permission = new \Common\Model\CustPermissionModel();
	}
    
	
	
	protected function _amountTakeNotes($custid , $fcustid , $amount , $leftamount , $subject , $businessid=0 , $memo='' , $code='' , $subtradeno=""){
		//记录流水
		$turnoverData = array();
		$turnoverData['custid'] = $custid;
		$turnoverData['fcustid'] = $fcustid;
		$turnoverData['amount'] = $amount;
		$turnoverData['subject'] = $subject;
		$turnoverData['businessid'] = empty($businessid) ? 0 : $businessid;
		$turnoverData['memo'] = $memo;
		$turnoverData['code'] = $code;
		$turnoverData['subtradeno'] = $subtradeno;
		$turnoverData['leftamount'] = $leftamount;
		$ret = $this->_purse_turnover->addTurnover($turnoverData);
		
		if($ret){
			return $ret;
		}else{
			return false;
		}
	}
	
	
	public function subjectListAction(){
		$subject[] = "发红包";
		$subject[] = "群费用支出";
		$subject[] = "群费用收入";
		$subject[] = "转账转入";
		$subject[] = "转账转出";
		$subject[] = "领取红包";
		$subject[] = "充值";
		$subject[] = "购物支付";
		$subject[] = "提现";
		$subject[] = "购物收款";
		
		$this->data = $subject;
		$this->outPut();
	}
	
	
	
	/**
	 * 设置支付密码
	 */
	public function resetPayPwdAction(){
		$this->_auth();
		$oldpwd = I('request.oldpwd');
		$newpwd = I('request.newpwd');
		$vercode = I('request.vercode');
		$time = I('request.time');
		$sign = I('request.sign');
		$this->_resetPayPwd($oldpwd,$newpwd,$vercode,$time,$sign);
	}
	
	
	private function _resetPayPwd($oldpwd,$newpwd,$vercode,$time,$sign){
		if(empty($newpwd) || empty($vercode) || empty($time) || empty($sign)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$this->_checkSign($this->_custInfo['mobile'],$time,$sign);
		$this->_checkVerCode($this->_custInfo['mobile'] , $vercode);
		
		$open_id = $this->_custInfo['ucropenid'];
		$ret = \ucr::setPayPass($open_id,$oldpwd,$newpwd);//print_r($ret);exit;
		if($ret['err_code'] === 0){
			$this->_delVerCode($this->_custInfo['mobile']);
			$this->outPut();
		}else{
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
	}
	
	
	
	public function bindBankCardAction(){
		$this->_auth();
		$bankcard = I('request.bankcard');
		$bankname = I('request.bankname');
		$accountname = I('request.accountname');
		$vercode = I('request.vercode');
		$time = I('request.time');
		$sign = I('request.sign');
		$this->_bindBankCard($bankcard,$bankname,$accountname,$vercode,$time,$sign);		
	}
	
	private function _bindBankCard($bankcard,$bankname,$accountname,$vercode,$time,$sign){
		if(empty($bankcard) || empty($bankname) || empty($accountname) || empty($vercode) || empty($time) || empty($sign)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$this->_checkSign($this->_custInfo['mobile'],$time,$sign);
		
		$this->_checkVerCode($this->_custInfo['mobile'] , $vercode);
		
		$amountData['custid'] = $this->_custInfo['custid'];
		$amountData['bankcard'] = $bankcard;
		$amountData['bankname'] = $bankname;
		$amountData['accountname'] = $accountname;
		$ret = $this->_purse->savePurse($amountData);
		if($ret){
			$this->_delVerCode($this->_custInfo['mobile']);
			$this->outPut();
		}else{
			$this->status = 2609;
			$this->msg = L('STATUS_2609');
			$this->outPut();
		}		
	}
	
	
	public function checkBankCardAction(){
		$bankcard = I('request.bankcard');
		$accountname = I('request.accountname');
		
		$type = "bankcard_name";
		$curl = curl_init();
		
		curl_setopt_array($curl, array(
		CURLOPT_URL => "http://v.apix.cn/apixcredit/idcheck/bankcard?type={$type}&bankcardno={$bankcard}&name={$accountname}&idcardno=&phone=",
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => "",
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 30,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => "GET",
		CURLOPT_HTTPHEADER => array("accept: application/json","apix-key: 844beceabb414e2d530e3bb0c8da0be9","content-type: application/json"),
		));
		
		$response = curl_exec($curl);
		$err = curl_error($curl);
		
		curl_close($curl);
		
		if ($err) {
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		} else {
			$response = json_decode($response , true);
			$this->status = $response['code'];
			$this->msg = $response['msg'];
			$this->outPut();
		}
	}
	
	
	public function unbindBankCardAction(){
		$this->_auth();
		$paypassword = I('request.paypassword');
		$time = I('request.time');
		$sign = I('request.sign');
		$this->_unbindBankCard($paypassword,$time,$sign);
	}
	
	private function _unbindBankCard($paypassword,$time,$sign){
		if(empty($paypassword) || empty($time) || empty($sign)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$this->_checkSign($this->_custInfo['mobile'],$time,$sign);
		
		//取支付token
		$open_id = $this->_custInfo['ucropenid'];
		$ret = \ucr::getPayToken($open_id,$paypassword);
		if(empty($ret['pay_token'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		
		$amountData['custid'] = $this->_custInfo['custid'];
		$amountData['bankcard'] = '';
		$amountData['bankname'] = '';
		$amountData['accountname'] = '';
		$ret = $this->_purse->savePurse($amountData);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2609;
			$this->msg = L('STATUS_2609');
			$this->outPut();
		}	
	}
	
	
	/**
	 * 我的银行卡
	 */
	public function myBankCardsAction(){
		$this->_auth();
		$this->_myBankCards();
	}
	
	private function _myBankCards(){
		$ret = $this->_purse->getPurse(array('custid'=>$this->_custInfo['custid']));
		$data['bankcard'] = $ret['bankcard'];
		$data['bankname'] = $ret['bankname'];
		$data['accountname'] = $ret['accountname'];
		$this->data = $data;
		$this->outPut();
	}
	
	
	
	//冻结帐号
	public function freezeAction(){
		$this->_auth();
		$custid = (int)I('request.custid');
		$this->_freeze($custid);
	}
	
	private function _freeze($custid){
		if(empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$ret = $this->_checkPermission($this->_custInfo['custid'] , 15);
		if(!$ret){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['status'] = 0;
		$ret = $this->_purse->savePurse($data);
		if($ret){
			//TODO api冻结帐号
			$this->outPut();
		}else{
			$this->status = 2612;
			$this->msg = L('STATUS_2612');
			$this->outPut();
		}
	}
	
	
	/**
	 * 开通支付
	 */
	public function openPayAction(){
		$this->_auth();
		$this->_openPay();
	}
	
	private function _openPay(){
		$open_id = $this->_custInfo['ucropenid'];
		$ret = \ucr::openPay($open_id);
		if($ret['err_code'] === 0){
			$this->outPut();
		}else{
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		
		$this->outPut();
	}
	
	
	/**
	 * 充值
	 */
	public function chargeAction(){
		$this->_auth();
		$amount = I('request.amount');
		$paytype = (int)I('request.paytype');
		$this->_charge($amount,$paytype);
	}
	
	private function _charge($amount,$paytype){
		if(empty($paytype)){
			$paytype = 1;//默认为支付宝充值
		}
		
		if(bccomp($amount, '0.01', 2) < 0){
			$this->status = 1001;
			$this->msg = "请输入充值金额.";
			$this->outPut();
		}
		
		if(bccomp($amount, C('MAX_CHARGE'), 2) > 0){
			$this->status = 1001;
			$this->msg = "充值金额不能大于" . C('MAX_CHARGE') . "元.";
			$this->outPut();
		}
		
		$data = array();
		$data['amount'] = $amount;
		$data['custid'] = $this->_custInfo['custid'];
		$data['status'] = 0;
		$chargeid = $this->_purse_charge->addCharge($data);
		if($chargeid){
			$this->data['chargeid'] = $chargeid;
			$title = "充值";
			$remark = "充值金额:{$amount}元";
			$notify_url = C('CHARGE_NOTIFY');
			$open_id = $this->_custInfo['ucropenid'];

            $makeRet = \ucr::makeImprest($open_id,$open_id,0,$amount*100,$title,$remark);
            $imprestCode = $makeRet['imprest_code'];
            $ret = \ucr::makePayV2($open_id,$this->_custInfo['custid'],"1",$chargeid,$imprestCode,$paytype,$notify_url);
			//$ret = \ucr::makePrepay($open_id,$chargeid,$amount*100,1,$paytype,'',$title,$remark,$notify_url);
			if($ret['err_code'] !== 0){
				$this->data = $ret;
				$this->outPut();
			}else{
				$this->status = $ret['err_code'];
				$this->msg = $ret['err_msg'];
				$this->outPut();
			}
		}else{
			$this->status = 2604;
			$this->msg = L('STATUS_2604');
			$this->outPut();
		}
	}
	
	
	private function _getBody(){
		$body = '';
		$fh   = @fopen('php://input', 'r');
		if ($fh)
		{
			while (!feof($fh))
			{
				$s = fread($fh, 1024);
				if (is_string($s))
				{
					$body .= $s;
				}
			}
			fclose($fh);
		}
		
		return $body;
	}


    /**
     * 支付回调
     */
    public function doPayAction(){
        $body = $this->_getBody();
        $this->_logError($body , "body");
        $body = json_decode($body , true);

        $tradeno = $body['out_trade_no'];
        $tradetype = $body['out_trade_type'];
        $title = $body['title'];
        $remark = $body['remark'];
        $amount = $body['amount'];
        $custid = $body['memo'];
        $open_id = $body['open_id'];
        $code = $body['imprest_code'];
        $this->_doPay($tradeno,$tradetype,$amount,$open_id,$code,$custid,$title,$remark);
    }

    public function _doPay($tradeno,$tradetype,$amount,$open_id,$code,$custid,$title="",$remark=""){
        if(empty($tradeno) || empty($amount) || empty($open_id) || empty($code)){
            $this->status = 1000;
            $this->msg = L('STATUS_1000');
            $this->outPut();
        }

        if ($tradetype=="1") { //充值
            $chargeInfo = $this->_purse_charge->getChargeById($tradeno);
            if(empty($chargeInfo) || $chargeInfo['status'] == "1" || $chargeInfo['amount']*100 != $amount*1){
                $this->status = 1001;
                $this->msg = L('STATUS_1001');
                $this->outPut();
            }
        }
        $leftamount = 0.00;
        $subtradeno = '';
        $changeAmount = 0;
        if ($tradetype=="1") { //充值
            $ret = \ucr::getPreAmountV2($open_id,$code,$amount,$title,$remark);
            if($ret['imprest_code'] !== ''){
                //$subtradeno = $ret['sub_trade_no'];
                $data['chargeid'] = $tradeno;
                $data['status'] = 1;
                $data['code'] = $code;
                $this->_purse_charge->updateCharge($data);

                $changeAmount = $amount/100;
            }else{
                $this->status = 1001;
                $this->msg = L('STATUS_1001');
                $this->outPut();
            }
        }else{
            $changeAmount = -$amount/100;
        }
        //记录流水
        $this->_amountTakeNotes($custid , 0 , $changeAmount , $leftamount , $title, 0 , $remark , $code , $subtradeno);

        echo '{"err_code":0,"err_msg":"OK"}';

    }
	
	/**
	 * 充值回调
	 */
	public function doChargeAction(){
		$body = $this->_getBody();
		$this->_logError($body , "body");
		$body = json_decode($body , true);
		
		$chargeid = $body['out_trade_no'];
		$amount = $body['amount'];
		$open_id = $body['open_id'];
		$code = $body['imprest_code'];
		$this->_doCharge($chargeid,$amount,$open_id,$code);
	}
	
	private function _doCharge($chargeid,$amount,$open_id,$code){
		if(empty($chargeid) || empty($amount) || empty($open_id) || empty($code)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$chargeInfo = $this->_purse_charge->getChargeById($chargeid);
		if(empty($chargeInfo) || $chargeInfo['status'] == "1" || $chargeInfo['amount']*100 != $amount*1){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$ret = \ucr::getPreAmountV2($open_id,$code,$amount,"充值","充值");
		if($ret['imprest_code'] !== ''){
			//$subtradeno = $ret['sub_trade_no'];
            $subtradeno = '';
			$leftamount = 0.00;
			
			
			$data['chargeid'] = $chargeid;
			$data['status'] = 1;
			$data['code'] = $code;
			$this->_purse_charge->updateCharge($data);
			
			//记录流水
			$this->_amountTakeNotes($chargeInfo['custid'] , 0 , $amount/100 , $leftamount , "充值" , 0 , '' , $code , $subtradeno);
			
			echo '{"err_code":0,"err_msg":"OK"}';
		}
	}
	
	
	/**
	 * 转账
	 */
	public function transferAction(){
		$this->_auth();
		$amount = I('request.amount');
		$custid = (int)I('request.custid');
		$paypassword = I('request.paypassword');
		$memo = I('request.memo');
		$this->_transfer($amount , $custid , $paypassword , $memo);
	}
	
	private function _transfer($amount , $custid , $paypassword , $memo=''){
		if(empty($custid) || empty($paypassword)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if(bccomp($amount, '0.01', 2) < 0){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$ret = $this->pay($custid , $amount , 0 , $paypassword ,array('转账转出','转账转入') , false , $memo);
		
		if(!empty($ret[0]) && !empty($ret[1])){//$ret1 && $ret2
			//发给接收方
			$msg['from_cust_name'] = $this->_custInfo['nickname'];
			$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
			$msg['to_cust_id'] = (int)$custid;
			$msg['type'] = 29;
			$msg['session_type'] = 1;
			$msg['session_id'] = (int)$this->_custInfo['custid'];
			$msg['memo'] = $memo;
			$msg['amount'] = $amount;
			$msg['turnoverid'] = $ret[1];
			\im::sendMsg(3, array($custid), array(), $msg);
			
			//发给自己
			$msg['from_cust_name'] = $this->_custInfo['nickname'];
			$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
			$msg['to_cust_id'] = (int)$custid;
			$msg['type'] = 29;
			$msg['session_type'] = 1;
			$msg['session_id'] = (int)$custid;
			$msg['memo'] = $memo;
			$msg['amount'] = $amount;
			$msg['turnoverid'] = $ret[0];
			\im::sendMsg(3, array($this->_custInfo['custid']), array(), $msg);
			
			$this->outPut();
		}else{
			$this->status = 2607;
			$this->msg = L('STATUS_2607');
			$this->outPut();
		}
	}
	
	
	/**
	 * 支付
	 */
	public function pay($custid , $amount , $businessid=0 , $paypassword='' , $subject = array() , $isReturn = true , $memo = ""){
		$this->_auth();
		if(empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if(bccomp($amount, '0.00', 2) < 0){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		if(bccomp($amount, '0.00', 2) == 0){
			if($isReturn)
				$this->outPut();
			else
				return array(0,0);
		}
		
		$open_id = $this->_custInfo['ucropenid'];
		
		$memoTmp = empty($memo)?$subject[0]:$memo;
		//支付
		//预付
		$ret = \ucr::makeImprest($open_id,'',1,$amount*100,$subject[0],$memoTmp);
		if(!empty($ret['err_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$code = $ret['imprest_code'];
		
		//取支付token
		$ret = \ucr::getPayToken($open_id,$paypassword);
		if(empty($ret['pay_token'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$pay_token = $ret['pay_token'];

        $notify_url = C("PAY_NOTIFY");
		//支付
        $ret =  \ucr::payImprestV2($custid,$open_id,$code,$notify_url,$pay_token);
		//$ret = \ucr::payImprest($pay_token,$open_id,$code,$subject[0],$memoTmp);
		if(!empty($ret['err_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
        $body = $ret['body'];
       $retBody =  json_decode($body , true);
        print_r($retBody);
		$subtradeno1 = $retBody['imprest_record_id'];
		$leftamount = 0.00;
		
		//记录转出方流水
		$ret1 = $this->_amountTakeNotes($this->_custInfo['custid'] , $custid , -$amount , $leftamount , $subject[0] , $businessid , $memoTmp , $code , $subtradeno1);
		
		
		//收入
		//绑定用户
		$ret = \ucr::bindUser($custid);
		if(empty($ret['open_id'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$open_id = $ret['open_id'];
		
		
		$memoTmp = empty($memo)?$subject[1]:$memo;
		//领取
		$ret = \ucr::getPreAmountV2($open_id,$code,$amount*100,$subject[1],$memoTmp);
		if(!empty($ret['err_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$subtradeno2 = $ret['imprest_record_id'];
		$leftamount = 0.00;
		
		//记录转入方流水
		$ret2 = $this->_amountTakeNotes($custid , $this->_custInfo['custid'] , $amount , $leftamount , $subject[1] , $businessid , $memoTmp , $code , $subtradeno2);
		
		if($ret1 && $ret2){
			if($isReturn) 
				$this->outPut();
			else 
				return array($subtradeno1,$subtradeno2);//return array($ret1,$ret2);
		}else{
			$this->status = 2606;
			$this->msg = L('STATUS_2606');
			$this->outPut();
		}
	}
	
	
	/**
	 * 提现
	 */
	public function cashAction(){
		$this->_auth();
		$amount = I('request.amount');
		$paypassword = I('request.paypassword');
		$this->_cash($amount , $paypassword);
	}
	
	private function _cash($amount , $paypassword=""){
		$bankCardInfo = $this->_purse->getPurse(array('custid'=>$this->_custInfo['custid']));
		if(empty($bankCardInfo['bankcard'])){
			$this->status = 2613;
			$this->msg = L('STATUS_2613');
			$this->outPut();
		}
		
		if(bccomp($amount, C('MIN_CASH'), 2) < 0){
			$this->status = 1001;
			$this->msg = "提现金额不能小于" . C('MIN_CASH') . "元.";
			$this->outPut();
		}
		
		$open_id = $this->_custInfo['ucropenid'];
		
		//预提现
		$ret = \ucr::makeCashout($open_id,$amount*100,"提现","提现");
		if(empty($ret['cashout_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$cashout_code = $ret['cashout_code'];
		
		
		//取支付token
		$ret = \ucr::getPayToken($open_id,$paypassword);
		if(empty($ret['pay_token'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$pay_token = $ret['pay_token'];
		
		//提现
		/* $ret = \ucr::cashout($open_id,$cashout_code,$pay_token);
		if(empty($ret['open_id'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		} */
		$subtradeno = "";//$ret['sub_trade_no'];
		$leftamount = 0.00;
		
		$ret = $this->_amountTakeNotes($this->_custInfo['custid'] , 0 , -$amount , $leftamount , "提现" , 0 , '' , $cashout_code , $subtradeno);
		if($ret){
			$data = array();
			$data['custid'] = $this->_custInfo['custid'];
			$data['amount'] = $amount;
			$data['status'] = 1;
			$cashid = $this->_purse_cash->addCash($data);
			
			$this->data['cashid'] = $cashid;
			$this->data['bankcard'] = $bankCardInfo['bankcard'];
			$this->data['bankname'] = $bankCardInfo['bankname'];
			$this->data['accountname'] = $bankCardInfo['accountname'];
			
			$this->outPut();
		}else{
			$this->status = 2603;
			$this->msg = L('STATUS_2603');
			$this->outPut();
		}
	}
	
	
	
	
	//提现列表
	public function getCashListAction(){
		$this->_auth();
		$pageSize = (int)I('request.pageSize');
		$pageIndex = (int)I('request.pageIndex');
		$status = (int)I('request.status');
		$this->_getCashList($pageSize,$pageIndex,$status);
	}
	
	private function _getCashList($pageSize,$pageIndex,$status){
		$ret = $this->_checkPermission($this->_custInfo['custid'] , 16);
		if(!$ret){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['status'] = $status;
		$cashList = $this->_purse_cash->getCashList($data);
		if(!empty($cashList['data'])){
			foreach($cashList['data'] as &$cash){
				$custInfo = $this->_getCustInfo($cash['custid']);
				$cash['university'] = $custInfo['university'];
				$cash['nickname'] = $custInfo['nickname'];
				$cash['realname'] = $custInfo['realname'];
				$cash['firstname'] = $custInfo['firstname'];
				$cash['secondname'] = $custInfo['secondname'];
			}
		}
		$this->data = $cashList;
		$this->outPut();
	}
	
	
	/**
	 * 钱包余额
	 */
	public function purseAction(){
		$this->_auth();
		$this->_purse();
	}
	
	private function _purse(){
		$open_id = $this->_custInfo['ucropenid'];
		$ret = \ucr::getAccount($open_id);
		if($ret['err_code'] !== 0){
			$data['amount'] = number_format($ret['amount']/100 , 2, '.', '');
			$data['status'] = $ret['status'];
			$data['_haspaypassword'] = $ret['password_is_set'];
			$this->data = $data;
			$this->outPut();
		}else{
			$this->status = $ret['err_code'];
			$this->msg = $ret['"err_msg'];
			$this->outPut();
		}
	}
	
	
	/**
	 * 钱包流水
	 */
	public function purseTurnoverAction(){
		$this->_auth();
		$subject = I('request.subject');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_purseTurnover($pageIndex , $pageSize,$subject);
	}
	
	private function _purseTurnover($pageIndex , $pageSize , $subject=''){
		$purseTurnover = array();
		$myPurseTurnover = \ucr::getAmountList($this->_custInfo['ucropenid'],$pageIndex,$pageSize,$subject);
        foreach($myPurseTurnover as $idx=>&$turnover){
            $purseTurnover[$idx]['id'] = $turnover['id'];
            $purseTurnover[$idx]['amount'] = $turnover['changed_amount']/100;
            $purseTurnover[$idx]['subject'] = $turnover['title'];
            $purseTurnover[$idx]['leftamount'] = $turnover['balance']/100;
            $purseTurnover[$idx]['tradeno'] = $turnover['imprest_record_id'];
            $purseTurnover[$idx]['createtime'] = $turnover['create_time'];
        }
		
		$this->data = $purseTurnover;
		$this->outPut();
	}
	
	
	
	/**
	 * 流水详情
	 */
	public function turnoverDetailAction(){
		$this->_auth();
		$turnoverid = I('request.turnoverid');
		$this->_turnoverDetail($turnoverid);
	}
	
	private function _turnoverDetail($turnoverid){
		$ret = \ucr::getAccountByTradeNoV2($this->_custInfo['ucropenid'],$turnoverid);
//		if(empty($ret['code'])){
//			$this->status = $ret['err_code'];
//			$this->msg = $ret['err_msg'];
//			$this->outPut();
//		}
		
		$turnoverInfo['turnoverid'] = $turnoverid;
		$turnoverInfo['amount'] = $ret['changed_amount']/100;
		$turnoverInfo['leftamount'] = $ret['balance']/100;
		$turnoverInfo['createtime'] = $ret['create_time'];
		$turnoverInfo['subject'] = $ret['title'];
		$turnoverInfo['memo'] = $ret['remark'];
		
		$turnover_Info = $this->_purse_turnover->getTurnoverByTradeno($turnoverid);
		if(!empty($turnover_Info)){
			$turnoverInfo['custid'] = $turnover_Info['custid'];
			$turnoverInfo['nickname'] = $this->_custInfo['nickname'];
			$turnoverInfo['mobile'] = $this->_custInfo['mobile'];
				
			$fcustInfo = $this->_getCustInfo($turnover_Info['fcustid']);
			$turnoverInfo['fcustid'] = $turnover_Info['fcustid'];
			$turnoverInfo['fnickname'] = $fcustInfo['nickname'];
			$turnoverInfo['fmobile'] = $fcustInfo['mobile'];
		}else{
			$turnoverInfo['custid'] = "0";
			$turnoverInfo['nickname'] = $this->_custInfo['nickname'];
			$turnoverInfo['mobile'] = $this->_custInfo['mobile'];
			
			$turnoverInfo['fcustid'] = "0";
			$turnoverInfo['fnickname'] = "";
			$turnoverInfo['fmobile'] = "";
		}
		
		$this->data = $turnoverInfo;
		$this->outPut();
	}
	
	
	

}
